package com.mrent.service;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;

import org.springframework.stereotype.Component;

import com.mrent.entity.Goods;
import com.mrent.entity.Message;

@Component
public class MessageServiceImpl implements MessageService {
	
	
	/**
	 * 保存消息.
	 * @param message
	 */
	public void saveMessage(Message message){
		EntityManager em = EMF.getEM();
		EntityTransaction trans = em.getTransaction();
		try{
			trans.begin();
			em.persist(message);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			trans.commit();
		}
	}
	
	/**
	 * 消息列表.
	 * 
	 * @param page
	 * @return
	 */
	public List<Message> listMessages(Page page){
		EntityManager em = EMF.getEM();
		Query query = em.createQuery("select from Message m ");
		page.setTotalCount(query.getResultList().size());
		query.setFirstResult(page.getFirst());
		query.setMaxResults(page.getPageSize());
		List<Message> list = query.getResultList();
		return list;
	}


}
